using UnityEngine;

namespace NodeCanvas.Actions
{
	[Name("Play Animation")]
	public class MecanimPlayAnimation : MecanimActions
	{
		public int layerIndex;

		[RequiredField]
		public string stateName;

		[SliderField(0f, 1f)]
		public float transitTime = 0.25f;

		public bool waitUntilFinish;

		private AnimatorStateInfo stateInfo;

		private bool played;

		protected override string info
		{
			get
			{
				return "Anim '" + stateName + "'";
			}
		}

		protected override void OnExecute()
		{
			played = false;
			AnimatorStateInfo currentAnimatorStateInfo = base.animator.GetCurrentAnimatorStateInfo(layerIndex);
			base.animator.CrossFade(stateName, transitTime / currentAnimatorStateInfo.length, layerIndex);
		}

		protected override void OnUpdate()
		{
			stateInfo = base.animator.GetCurrentAnimatorStateInfo(layerIndex);
			if (waitUntilFinish)
			{
				if (stateInfo.IsName(stateName))
				{
					played = true;
					if (base.elapsedTime >= stateInfo.length / base.animator.speed)
					{
						EndAction();
					}
				}
				else if (played)
				{
					EndAction();
				}
			}
			else if (base.elapsedTime >= transitTime)
			{
				EndAction();
			}
		}
	}
}
